<template>

  <div class="app-container">
    <div class="filter-container">
      <el-button class="pan-btn blue-btn" @click="submitSelectValue">提交选中数据</el-button>
    </div>
    <el-table
      :data="tableData"
      height="600"
      @selection-change="selectionChange"
      style="width: 100%">
      <el-table-column
        type="selection"
        width="55">
      </el-table-column>
      <el-table-column
        prop="columnName"
        label="字段名称"
        width="180">
      </el-table-column>
      <el-table-column
        prop="dataType"
        label="数据类型"
        width="180">
      </el-table-column>
      <el-table-column
        prop="columnDesc"
        label="字段说明">
      </el-table-column>
      <el-table-column
        prop="columnAlias"
        label="字段别名">
      </el-table-column>
      <el-table-column label="是否指标">
        <template slot-scope="scope">
          <el-switch v-if="scope.row.isdimension==1"
                     disabled
                     v-model="scope.row.isquota"
                     active-color="#13ce66"
                     inactive-color="#ff4949"
                     :active-value=1
                     :inactive-value=0>
          </el-switch>
          <el-switch v-else
                     v-model="scope.row.isquota"
                     active-color="#13ce66"
                     inactive-color="#ff4949"
                     :active-value=1
                     :inactive-value=0>
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="是否维度">
        <template slot-scope="scope">
          <el-switch
            v-if="scope.row.isquota==1"
            v-model="scope.row.isdimension"
            active-color="#13ce66"
            inactive-color="#ff4949"
            disabled
            :active-value=1
            :inactive-value=0>
          </el-switch>
          <el-switch
            v-else
            @change="switchEvent($event,scope.row)"
            v-model="scope.row.isdimension"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value=1
            :inactive-value=0>
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column
        prop="updateDate"
        label="更新时间">
      </el-table-column>
    </el-table>


    <el-dialog
      title="维度"
      :visible.sync="dimensionAttr.centerDialogVisible"
      width="30%"
      v-dialogDrag
      center>
      <div>
        <div style="margin: 15px 0;"></div>
        <el-checkbox-group v-model="dimensionAttr.checkList">
          <el-checkbox border v-for="item in dimensionAttr.list" :label="item.id" :key="item.id">
            {{ item.dimensionName }}
          </el-checkbox>
        </el-checkbox-group>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleCheckButton(1)">取 消</el-button>
        <el-button type="primary" @click="handleCheckButton(2)">确 定</el-button>
      </span>
    </el-dialog>
  </div>

</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
import { getColAlias } from '@/api/quota/define/define.js'
import { listDimension } from '@/api/metadata/dimension'
//例如：import 《组件名称》 from '《组件路径》';
export default {
  name: 'columnAlias',
  //使用import引入外部组件需要在components对象中注册才可以使用
  components: {},
  props: ['parentTableNameAliasId', 'themeId'],
  data() {
    //这里存放数据
    return {
      dimensionAttr: {
        centerDialogVisible: false,
        isIndeterminate: true,
        checkAll: false,
        queryParams: {
          current: 1,
          size: 10000,
          upperId: 0
        },
        list: [],
        checkList: [],
        isDimension:undefined
      },
      tableData: [],
      selectData: [],
      routeData: {
        quota: [],
        dimension: [],
        themeId: this.themeId
      },
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderField: 'UPDATE_DATE',
        order: 'desc',
        tablenameAliasId: ''
      }
    }
  },
  //计算属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {
    parentTableNameAliasId(newVal, oldVal) {
      if (newVal != undefined && newVal != null) {
        this.queryParams.tablenameAliasId = newVal
        this.getColAliasList()
      }
    },
    themeId(nv, ov) {
      this.themeId = nv
    }
  },
  //方法集合
  methods: {
    switchEvent(event, val) {
      if (event === 1) {
        this.dimensionAttr.centerDialogVisible = true
        //查询一级维度
        listDimension(this.dimensionAttr.queryParams).then(res => {
          this.dimensionAttr.list = res.data.records
        })
      }
    },
    handleCheckButton(val) {
      if (val === 1) {
        this.dimensionAttr.centerDialogVisible = false
        this.dimensionAttr.checkList = []
        return
      }else{
        //确定后调用接口修改字段别名表 保存数据 刷新列表(必须)   已选维度展示 自动勾选？还是取消删除已关联的？


      }

    },
    submitSelectValue() {
      let quotaLength = this.routeData.quota.length
      if (quotaLength <= 0) {
        this.msgError('没有指标数据可提交')
      } else {
        this.$router.push({ name: 'pretreatmentDefine', params: this.routeData })
      }

      // else if (quotaLength > 0 && dimensionLength <= 0) { //如果只有指标数据跳转指标界面
      //   this.$router.push({ name: 'pretreatmentDefine', params: this.routeData })
      // } else if (quotaLength <= 0 && dimensionLength > 0) { //如果只有维度数据直接跳转维度
      //   this.$router.push({ name: 'pretreatmentDimension', params: this.routeData })
      // } else {//否则先处理指标在处理维度
      //   this.$router.push({ name: 'pretreatmentDefine', params: this.routeData })
      // }
    },
    selectionChange(selection) {
      if (selection.length > 0) {
        selection.forEach(item => {
          if (item.isquota == 1) {
            this.routeData.quota.push(item)
          } else if (item.isdimension == 1) {
            this.routeData.dimension.push(item)
          }
        })
      }
    },
    getColAliasList() {
      if (this.parentTableNameAliasId != undefined && this.parentTableNameAliasId != null) {
        getColAlias(this.queryParams).then((res) => {
          this.tableData = res.data.map(item => {
            this.$set(item, 'isQuota', 0)
            return item
          })
        })
      }
    }
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {
    this.getColAliasList()
  },
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {
  },
  beforeCreate() {
  }, //生命周期 - 创建之前
  beforeMount() {
  }, //生命周期 - 挂载之前
  beforeUpdate() {
  }, //生命周期 - 更新之前
  updated() {
  }, //生命周期 - 更新之后
  beforeUnmount() {
  }, //生命周期 - 销毁之前
  unmounted() {
  }, //生命周期 - 销毁完成
  activated() {
  } //如果页面有keep-alive缓存功能，这个函数会触发
}
</script>
<style scoped>
</style>
